package com.example.RuanJianKeShe.Dao.impl;

import com.example.RuanJianKeShe.Bean.Course;
import com.example.RuanJianKeShe.Dao.CourseDao;
import com.example.RuanJianKeShe.Utils.JDBCUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class CourseDaoImpl implements CourseDao {
    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());



    /**
     * 获取所有课程信息
     *
     * @return List<Course>
     */
    @Override
    public List<Course> getAllCourse() {
        String sql = "select * from course";
        try {
            List<Course> course = template.query(sql,new BeanPropertyRowMapper<Course>(Course.class));
            return course;
        } catch (DataAccessException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 通过id获取课程信息
     *
     * @param id int
     * @return Course
     */
    @Override
    public Course getCourseById(int id) {
        String sql = "select * from course where cno = ?";
        try {
            Course course = template.queryForObject(sql,new BeanPropertyRowMapper<Course>(Course.class),id);
            return course;
        }catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 通过id修改课程信息
     *
     * @param course Course
     * @return boolean
     */
    @Override
    public Boolean setCourseById(Course course) {
        String sql = "update course set cname=?,credit=?,classId=?,capacity=?,address=?,createTime=? where cno=?";
        try{
            template.update(sql,course.getCname(),course.getCredit(),course.getClassId(),
                    course.getCapacity(),course.getAddress(),course.getCreateTime(),course.getCno());
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 添加课程信息
     *
     * @param course Course
     * @return boolean
     */
    @Override
    public Boolean addCourse(Course course) {
        String sql = "insert into course(cname,credit,classId,capacity,address,createTime) values(?,?,?,?,?,?)";
        try {
            template.update(sql,course.getCname(),course.getCredit(),course.getClassId(),
                    course.getCapacity(),course.getAddress(),course.getCreateTime());
            return true;
        }catch (Exception e){
            e.printStackTrace();

        }
        return false;

    }

    /**
     * 通过id删除课程信息
     *
     * @param id int
     * @return boolean
     */
    @Override
    public Boolean delCourseById(int id) {
        String sql = "delete from course where cno = ?";
        try {
            template.update(sql,id);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 删除所有课程信息
     *
     * @return boolean
     */
    @Override
    public Boolean delAllCourse() {
        String sql = "delete from course";
        try {
            template.update(sql);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 通过id列表删除多条课程
     *
     * @param idList int[]
     * @return boolean
     */
    @Override
    public Boolean delCourseByIdList(int[] idList) {
        String sql = "delete from course where cno = ?";
        try {
            for (int i = 0; i < idList.length; i++) {
                template.update(sql, idList[i]);
            }
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }
}
